package ${package.ServiceImpl};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${superServiceImplClassPackage};
import ${superServiceClassPackage};
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.liqi.common.core.domain.PageData;
import com.liqi.common.core.domain.PageParam;
import com.liqi.common.core.domain.R;
import com.liqi.common.utils.poi.POIExcelUtil;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author ${author}
 * @date ${date}
 */
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${superServiceClass}<${entity}> {

    @Autowired
    private ${table.mapperName} ${table.mapperName?uncap_first};

    // 分页
    public R<PageData<${entity}>> page(PageParam<${entity}> pageParam) {
        ${entity} ${entity?uncap_first} = pageParam.getData();
        QueryWrapper<${entity}> qw = new QueryWrapper<>();
        if (${entity?uncap_first} != null) {
            // 条件
        }
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy());
        List<${entity}> list = ${table.mapperName?uncap_first}.selectList(qw);
        return R.ok(PageData.page(PageInfo.of(list)));
    }

    // 获取
    public R<${entity}> get(Integer id) {
        ${entity} ${entity?uncap_first} = ${table.mapperName?uncap_first}.selectById(id);
        return R.ok(${entity?uncap_first});
    }

    // 新增
    @Transactional
    public R add(${entity} ${entity?uncap_first}) {
        ${table.mapperName?uncap_first}.insert(${entity?uncap_first});
        return R.ok();
    }

    // 修改
    @Transactional
    public R modify(${entity} ${entity?uncap_first}) {
        int i = ${table.mapperName?uncap_first}.updateById(${entity?uncap_first});
        return i > 0 ? R.ok() : R.fail("修改失败");
    }

    // 删除
    @Transactional
    public R delete(Integer id) {
        int i = ${table.mapperName?uncap_first}.deleteById(id);
        return i > 0 ? R.ok() : R.fail("删除失败");
    }

    // excel标题
    private static final String[] headers = {
        <#list table.fields as field>
            "${field.comment}",
        </#list>
    };

    // excel标题属性
    private static final String[] fieldNames = {
        <#list table.fields as field>
            "${field.propertyName}",
        </#list>
    };

    // 导出excel
    public void exportExcel(${entity} ${entity?uncap_first}, HttpServletRequest request, HttpServletResponse response) {
        QueryWrapper<${entity}> qw = new QueryWrapper<>();
        // 条件
        List<${entity}> dataList = ${table.mapperName?uncap_first}.selectList(qw);
        String fileName = "${table.comment!}";
        try {
            POIExcelUtil.exportExcel(response, request, fileName, headers, fieldNames, dataList, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 导入excel
    public void importExcel(MultipartFile file) {
        try {
            List<${entity}> dataList = POIExcelUtil.importExcel(file, fieldNames, ${entity}.class);
            // 业务逻辑待写
            for (${entity} ${entity?uncap_first} : dataList) {
                if (${entity?uncap_first}.get${table.fields[0].propertyName?cap_first}() == null) {
                    ${table.mapperName?uncap_first}.insert(${entity?uncap_first});
                } else {
                    ${table.mapperName?uncap_first}.updateById(${entity?uncap_first});
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 下载导入excel模板
    public void downloadImportExcel(HttpServletRequest request, HttpServletResponse response) {
        List<${entity}> dataList = Collections.emptyList();
        String fileName = "${table.comment!}";
        try {
            POIExcelUtil.exportExcel(response, request, fileName, headers, fieldNames, dataList, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
</#if>
